package com.conoic.sxs.xcj.gateway.web.controller.base;

import com.conoic.sxs.xcj.gateway.common.util.json.JsonUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.UnknownHostException;

public class BaseController {

    private static final Logger logger = LoggerFactory.getLogger(BaseController.class);

    /**
     * The constant IPV4_LOCAL.
     */
    private static final String IPV4_LOCAL = "127.0.0.1";
    /**
     * The constant IPV6_LOCAL.
     */
    private static final String IPV6_LOCAL = "0:0:0:0:0:0:0:1";
    /**
     * The constant IP_UNKNOWN.
     */
    private static final String IP_UNKNOWN = "unknown";


    public <T> String toJson(T t) {

        try {
            String json = JsonUtil.toJSON(t);
            logger.info("接口响应的数据response=[{}]", json);
            return json;
        } catch (Exception e) {
            logger.info("接口返回数据转为json格式错误:", e);
            return null;
        }


    }


    /**
     * Gets the ip addr. 获取访问者真实IP地址
     *
     * @param request the request
     * @return the ip addr
     */
    public static String getIp(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || IP_UNKNOWN.equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || IP_UNKNOWN.equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || IP_UNKNOWN.equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
            if (ip.equals(IPV4_LOCAL) || ip.equals(IPV6_LOCAL)) {
                // 根据网卡取本机配置的IP
                InetAddress inet = null;
                try {
                    inet = InetAddress.getLocalHost();
                } catch (UnknownHostException e) {
                }
                if (inet != null) {
                    ip = inet.getHostAddress();
                }
            }
        }
        // 对于通过多个代理的情况，第一个IP为客户端真实IP,多个IP按照','分割
        if (StringUtils.isNotBlank(ip) && ip.contains(",")) { // "***.***.***.***".length()
            ip = ip.substring(0, ip.indexOf(','));
        }
        return ip;
    }

    /**
     * 获取请求参数
     *
     * @param request the request
     * @return data
     */
    protected String getData(HttpServletRequest request) {
        BufferedReader reader = null;
        StringBuilder sb = new StringBuilder();
        try {
            reader = new BufferedReader(new InputStreamReader(request.getInputStream(), "utf-8"));
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != reader) {
                    reader.close();
                }
            } catch (Exception e) {

            }
        }
        return sb.toString();
    }

}
